package com.serverSite;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.bdd.ConMySql;

public class Processing {
	public Processing() {
		super();
	}
	/**
	 * 
	 * @param wanted
	 * @param productQty
	 * @param bookedQty
	 * @return the quantity available for a product (int)
	 */
	public int bookingTest(int wanted,int productQty,int bookedQty){
		int available=0;
		available = productQty - bookedQty;
		if(available<=0)
			available = 0;

		return available;
	}
	/**
	 * create a json object to be sent to the servlet and set the db at good values
	 * @param jsonO
	 * @param rs
	 * @param con
	 * @return a json object
	 * @throws JSONException
	 * @throws SQLException
	 */
	public JSONObject bookingProcess(JSONObject jsonO, ResultSet rs,ConMySql con) throws JSONException, SQLException{
		int wanted = jsonO.getInt("wantedQty");
		rs.next();
		int productQty = rs.getInt("productQty");
		int bookedQty = rs.getInt("bookedQty");
		int productId = rs.getInt("productId");
		int availableQty = this.bookingTest(wanted, productQty, bookedQty);
		jsonO.put("message", "booked");
		jsonO.put("productId", productId);
		jsonO.put("availableQty", availableQty);
		if(wanted <= availableQty){
			con.execute("update product set bookedQty = bookedQty+"+wanted+" where productId = "+productId+" ;");
			jsonO.put("booked", wanted);
		}
		else{
			int booked = wanted-availableQty;
			con.execute("update product set bookedQty = productQty where productId = "+productId+" ;");
			jsonO.put("booked", booked);
		}
		return jsonO;
	}
	
	public JSONObject buyingProcess(JSONObject jsonO, ResultSet rs,ConMySql con) throws JSONException, SQLException{
		int wanted = jsonO.getInt("wantedQty");
		jsonO=new JSONObject();
		rs.next();
		int productQty = rs.getInt("productQty");
		int bookedQty = rs.getInt("bookedQty");
		int productId = rs.getInt("productId");
		int availableQty = this.bookingTest(wanted, productQty, bookedQty);
		jsonO.put("message", "bought");
		jsonO.put("productId", productId);
		jsonO.put("availableQty", availableQty);
		if(wanted <= availableQty){
			con.execute("update product set bookedQty = bookedQty-"+wanted+" where productId = "+productId+" ;");
			con.execute("update product set productQty = productQty-"+wanted+" where productId = "+productId+" ;");
		}
		else{
			con.execute("update product set bookedQty = bookedQty-"+wanted+" where productId = "+productId+" ;");
			con.execute("update product set productQty = 0 where productId = "+productId+" ;");
		}
		return jsonO;
	}
	
	public void cancelProcess(JSONArray jsonArray, int productId){
		System.out.println("########### ( jsonarray ) "+jsonArray);
		int size = jsonArray.length();
		for(int i = 0; i < size;i++){
			try {
				if(!jsonArray.get(i).equals(null)){
					int server;
					int qty;
					try {
						JSONObject js = jsonArray.getJSONObject(i);
						server = js.getInt("server");
						qty = js.getInt("qty");
						ConMySql con = new ConMySql("bddSite"+server);
						con.execute("update product set bookedQty = bookedQty-"+qty+" where productId = "+productId+" ;");
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}	
		}
	}

	
	
	/**
	 * raz for setting all db at initial values
	 */
	public void raz(){
		for(int i = 10001;i<10006;i++){
			ConMySql con = new ConMySql("bddSite"+i);
			String raz = "update product set productQty = 1000 where productId in (1,2,3,4,5);";
			String raz2 = "update product set bookedQty = 0 where productId in (1,2,3,4,5);";
			con.execute(raz);
			con.execute(raz2);
		}
	}
}
